package com.wisdomparking.service.siwo.impl;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wisdomparking.service.siwo.SiwoDataService;
import com.wisdomparking.service.siwo.resbean.Res;
import com.wisdomparking.tools.AbstractCasProtocolUrlBasedTicketValidator;
import com.wisdomparking.tools.HttpRespons;
import com.wisdomparking.tools.ToolHTTP;
import com.wisdomparking.tools.ToolValid;

@Service
public class SiwoDataServiceImpl implements SiwoDataService {
	
	String Token = "";
	long updateTime = 0;
	
	
	
	public SiwoDataServiceImpl() {
		long inval = System.currentTimeMillis() - updateTime;
		if(ToolValid.isEmpty("") || inval > 30 * 60 * 1000){
			login();
		}
	}

	public String getRes(String name, String queryString) {
		
		HttpRespons res = null;
		
		try {
			String base = "http://api.cnpms.cn/v407/";
			ToolHTTP.setDefaultContentEncoding("utf-8");
			res = ToolHTTP.sendGet(base + name + "?" + queryString);
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		return res.getContent();
	}
	
	public String getRes2(String name, String queryString) {
		
		String res = null;
		
		try {
			String base = "https://api.cnpms.cn/v479/";
			// ToolHTTP.setDefaultContentEncoding("utf-8");
			// System.out.println(base + name + "&" + queryString);
			// res = ToolHTTP.sendGet(base + name + "&" + queryString);
			String url = base + name + "&" + queryString;
			AbstractCasProtocolUrlBasedTicketValidator https = new AbstractCasProtocolUrlBasedTicketValidator();
			res = https.retrieveResponseFromServer(new URL(url), null);
			// System.out.println("res: " + res);
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public String getResContentByKey(Res res, String key){
		String content = "";
		
		if(ToolValid.isNotEmpty(res) && 0 == res.getCode()){
			Gson gson = new Gson();
			Map<String, Object> map = gson.fromJson(gson.toJson(res.getData()), new TypeToken<HashMap<String,Object>>(){}.getType());  
			if(null!=map.get(key)){
				content = map.get(key).toString();
			}
		}
		
		return content;
	}
	
	
	@Override
	public Res login() {
		String queryString = "userid=Manager&password=202CB962AC59075B964B07152D234B70";
		Res res = new Gson().fromJson(getRes("Login", queryString), Res.class);
		
		if(0 == res.getCode()){
			Gson gson = new Gson();
			Map<String, Object> map = gson.fromJson(gson.toJson(res.getData()), new TypeToken<HashMap<String,Object>>(){}.getType());  
			if(null!=map.get("Token")){
				Token = map.get("Token").toString();
				updateTime = System.currentTimeMillis();
			}
		}
		
		return res;
	}


	@Override
	public Res logout() {
		String queryString = "token=" + Token;
		return new Gson().fromJson(getRes("Logout", queryString), Res.class);
	}
	
	@Override
	public Res getDoors() {
		String queryString = "token=" + Token;
		return new Gson().fromJson(getRes("GetDoors", queryString), Res.class);
	}
	
	public Res getDoorHVs(String doorid, int io){
		String queryString = "token=" + Token;
		queryString = queryString + "&doorid=" + doorid;
		queryString = queryString + "&io=" + io;
		
		return new Gson().fromJson(getRes("GetDoorHVs", queryString), Res.class);
	}
	
	public Res getCarOwnerProfiles(String plate){
		String queryString = "token=" + Token;
		queryString = queryString + "&plate=" + plate;
		
		return new Gson().fromJson(getRes("GetCarOwnerProfiles", queryString), Res.class);
	}
	
	public Res payMonthly(String id, String money, String valid){
		String queryString = "token=" + Token;
		queryString = queryString + "&id=" + id;
		queryString = queryString + "&money=" + money;
		queryString = queryString + "&valid=" + valid;
		
		return new Gson().fromJson(getRes("PayMonthly", queryString), Res.class);
	}
	
	public Res recharge(String id, String money){
		String queryString = "token=" + Token;
		queryString = queryString + "&id=" + id;
		queryString = queryString + "&money=" + money;
		
		return new Gson().fromJson(getRes("Recharge", queryString), Res.class);
	}
	
	public Res carIn(String plate, String platecolor, String hvid){
		String queryString = "token=" + Token;
		queryString = queryString + "&plate=" + plate;
		queryString = queryString + "&platecolor=" + platecolor;
		queryString = queryString + "&hvid=" + hvid;
		
		return new Gson().fromJson(getRes("CarIn", queryString), Res.class);
	}
	
	public Res getParkingFee(String plate, String platecolor, String discountid){
		String queryString = "token=" + Token;
		queryString = queryString + "&from=zizhuji";
		queryString = queryString + "&plate=" + plate;
		queryString = queryString + "&platecolor=" + platecolor;
		queryString = queryString + "&discountid=" + discountid;
		
		return new Gson().fromJson(getRes("GetParkingFee", queryString), Res.class);
	}
	
	public Res payParkingFee(){
		return null;
	}
	
	public Res getPayRecords(String appid, String openid, String paytype, String page, String pagecount){
		String queryString = "token=" + Token;
		queryString = queryString + "&appid=" + appid;
		queryString = queryString + "&openid=" + openid;
		queryString = queryString + "&paytype=" + paytype;
		queryString = queryString + "&page=" + page;
		queryString = queryString + "&pagecount=" + pagecount;
		return new Gson().fromJson(getRes("GetPayRecords", queryString), Res.class);
	}
	
	public Res verifyBarcode(String barcode){
		String queryString = "token=" + Token;
		queryString = queryString + "&barcode=" + barcode;
		return new Gson().fromJson(getRes("VerifyBarcode", queryString), Res.class);
	}
	
	public String getGetParkingSpaces() {
		String queryString = "token=" + Token;
		return getRes2("default.ashx?action=GetParkingSpaces", queryString);
	}

}
